<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/12/22
 * Time: 10:36
 */

namespace app\commi\controller;


use think\Db;
use think\Request;

class Fac extends Base
{
    public $match_id;
    
    public function __construct()
    {
        parent::__construct();
        $this->match_id = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->where('match_member_id', session('cm_id'))
            ->value('match_id');
    }
    
    /**
     * 定位设备展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up__sensor')
                ->where('app_match_id', $this->match_id)
                ->count();// 获取总条数
            $lists = Db::table('up__sensor')
                ->field('app_id, app_sb_id, app_number')
                ->where('app_match_id', $this->match_id)
                ->page($Nowpage,$limits)
//                ->order('mag_id desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加定位设备
     * @return mixed|\think\response\Json
     */
    public function addSensor(){
        if(Request::instance()->isPost()){
            if(!$this->match_id) return json(['code' => 100, 'msg' => '请先创建赛事']);
            $app_sb_id = trim(input('app_sb_id'), ' ');
            $is_have = Db::table('up__sensor')
                ->where('app_sb_id', $app_sb_id)
                ->where('app_match_id', $this->match_id)
                ->find();
            if($is_have){
                return json(['code' => 100, 'msg' => '设备ID重复']);
            }
            $number     = rand(1000, 9999);
            $app_id     = $this->getNumber($number);
            $sensor = [
                'app_id' => $app_id,
                'app_sb_id' => $app_sb_id,
                'app_number' => $app_id,
                'app_match_id' => $this->match_id
            ];
            $res = Db::table('up__sensor')->insert($sensor);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $fac = Db::table('up_fac')->field('fac_id, fac_number')->select();
        $this->assign('fac', $fac);
        return $this->fetch('fac/addsensor');
    }
    
    /**
     * 删除定位设备
     * @return \think\response\Json
     */
    public function delSensor(){
        $app_sb_id = input('id');
        $res = Db::table('up__sensor')
            ->where('app_sb_id', $app_sb_id)
            ->where('app_match_id', $this->match_id)
            ->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 产生随机数
     * @param $number
     * @return mixed
     */
    public function getNumber($number)
    {
        $device = Db::table('up__sensor')
            ->where('app_number', $number)
            ->find();
        if ($device) {
            $num = rand(1000, 9999);
            return $this->getNumber($num);
        } else {
            return $number;
        }
    }
}